<?php

    /**
     *      [Discuz!] (C)2001-2099 Comsenz Inc.
     *      This is NOT a freeware, use is subject to license terms
     *      $Id: table_mobile_setting.php 31281 2012-08-03 02:29:27Z zhangjie $
     */

    if (!defined('IN_DISCUZ')) {
        exit('Access Denied');
    }

    class table_cloud_ios_push extends discuz_table {

        public function __construct() {
            $this->_table = 'cloud_ios_push';
            $this->_pk = 'id';

            parent::__construct();
        }

        //插入数据库
        public function insert($arr) {
            return DB::insert($this->_table, $arr, true);
        }

        //更新数据库
        public function update($id, $data) {
            $id = intval($id);
            if (!$data) {
                return;
            }
            return DB::update($this->_table, $data, DB::field($this->_pk, $id));
        }

        //查找某条记录
        public function fetch_push_info($id) {
            $id = intval($id);
            return DB::fetch_first("SELECT * FROM %t WHERE id=%d", array($this->_table, $id));
        }

        //查找某条记录
        public function fetch_push_info_by_udid($udid,$caid) {
            return DB::fetch_first("SELECT * FROM %t WHERE udid=%d and caid=%d ", array($this->_table, $udid,$caid));
        }

        //查找某条记录
        public function fetch_push_all_by_udid($udid,$caid) {

            return DB::fetch_all("SELECT * FROM %t WHERE udid=%s and caid=%d ", array($this->_table, $udid,$caid));
        }

        public function  fetch_all_by_caid($caid){
            return DB::fetch_all("SELECT * FROM %t WHERE caid=%d ", array($this->_table, $caid));
        }

        public function  fetch_all_find($caid, $start = 0, $limit = 0){
            return DB::fetch_all('SELECT push.*,users.name FROM '.DB::table($this->_table).' as push  left join '.DB::table('cloud_users').' as users on push.cuid = users.cuid where push.caid='.$caid. DB::limit($start, $limit));
        }
        //查询count
        public function fetch_all_count($caid) {
            return DB::fetch_first('SELECT count(id) as count FROM ' . DB::table($this->_table) . ' where caid='. $caid);
        }
        //查询所有推送列表
        public function  push_all_find($caid, $start = 0, $limit = 0){
            return DB::fetch_all("SELECT * FROM (select caid,cuid,udid,lasttime,versions,province,city,operator,brand,'ios' as type from ".DB::table('cloud_ios_push')." union select caid,cuid,udid,lasttime,versions,province,city,operator,brand,'android' as type from ".DB::table('cloud_android_push').') as push where push.caid='.$caid .' order by '.DB::order('lasttime', 'desc'). DB::limit($start, $limit));
        }
        //查询所有推送列表
        public function  push_all_find_count($caid){
            return DB::fetch_first("SELECT count(id) as count FROM (select id,caid,'ios' as type from ".DB::table('cloud_ios_push')." union select id,caid,'android' as type from ".DB::table('cloud_android_push').') as push where push.caid='.$caid);
        }
        //查询用户名
        public function  getUserName($cuid){
            return DB::fetch_first("SELECT name FROM ".DB::table('cloud_users')." where cuid=".$cuid);
        }
        //按条件筛选
        public function  get_ios_by_where($where){
            return DB::fetch_all('SELECT * FROM '.DB::table($this->_table).$where);
        }
        //默认筛选推送（ios/android）
        public function  get_all_by_where($where){
            return DB::fetch_all('SELECT * FROM '.DB::table($this->_table).$where);
        }
    }